//
//  GridPersonnelAddView.swift
//  grid-ios-app
//
//  Created by wondertek on 2024/4/3.
//

import UIKit
import SnapKit

internal protocol GridPersonnelAddViewDelegate: NSObjectProtocol {
    
    /// 0男，1女
    func gridPersonnelAddViewClickGender(tag :Int)
    
    // type 1 姓名， Type 2手机号码
    func gridPersonnelAddViewTextFieldChange(text :String?, type: Int)
    
    // type 1所属网格 2 职位
    func gridPersonnelAddViewSelectedClick(type: Int)

    // 保存
    func gridPersonnelAddViewSaveClick()

}

class GridPersonnelAddView: UIView {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

    internal weak var delegate: GridPersonnelAddViewDelegate?

    var persionModel: GridPersonnelCellModel? {
        didSet {
            gridModel = persionModel?.gridModel
            nameTextField.text = persionModel?.name
            phoneTextField.text = persionModel?.phoneNum
            if let ge: String = persionModel?.gender {
                if ge == "男" {
                    segmentedSender.selectedSegmentIndex = 0

                } else {
                    segmentedSender.selectedSegmentIndex = 1
                }
            }
            positionMessLabel.text = persionModel?.management
        }
    }

    
    var gridModel: GridModel? {
        didSet {
            managerMessLabel.text = gridModel?.name ?? ""
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    
    // 基本信息
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(12)
        label.text = "基本信息"
        label.backgroundColor = .clear
        label.textAlignment = .left
        addSubview(label)
        return label
    }()
    
    private lazy var lineView1: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        addSubview(view)
        return view
    }()
    
    // 人员姓名
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "人员姓名"
        label.backgroundColor = .clear
        label.textAlignment = .left
        addSubview(label)
        return label
    }()
    
    lazy var nameTextField: UITextField = {
        let textField = UITextField()
        textField.placeholder = "请输入人员名称"
        textField.font = Theme.Font.size(14)
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.addTarget(self, action: #selector(nameTextFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        addSubview(textField)
        return textField
    }()
    
    private lazy var lineView2: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        addSubview(view)
        return view
    }()
    
    // 手机号
    private lazy var phoneLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "手机号"
        label.backgroundColor = .clear
        label.textAlignment = .left
        addSubview(label)
        return label
    }()
    
    lazy var phoneTextField: UITextField = {
        let textField = UITextField()
        textField.placeholder = "请输入手机号"
        textField.font = Theme.Font.size(14)
        textField.keyboardType = .numberPad
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.addTarget(self, action: #selector(nameTextFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        addSubview(textField)
        return textField
    }()
    
    private lazy var lineView3: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        addSubview(view)
        return view
    }()
    
    // 性别
    private lazy var senderLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "性别"
        label.backgroundColor = .clear
        label.textAlignment = .left
        addSubview(label)
        return label
    }()
    
    private lazy var segmentedSender: UISegmentedControl = {
        let segmentedControl = UISegmentedControl(items: ["男", "女"])
        // 选项颜色
        segmentedControl.tintColor = UIColor.blue
        // 默认选中第二项
        segmentedControl.selectedSegmentIndex = 0
        // 点击事件
        segmentedControl.addTarget(self, action: #selector(segmentedControlChange(_ :)), for: .valueChanged)
        // 2
        segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: UIControl.State.normal)
        // 3
        segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.blue], for: UIControl.State.selected)
        // 4
        segmentedControl.backgroundColor = .white
        addSubview(segmentedControl)
        return segmentedControl
    }()
    
    private lazy var lineView4: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        addSubview(view)
        return view
    }()
    
    // 所属网格
    private lazy var managerLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "所属网格"
        label.backgroundColor = .clear
        label.textAlignment = .left
        addSubview(label)
        return label
    }()
    
    private lazy var managerMessLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = ""
        label.backgroundColor = .clear
        label.textAlignment = .right
        addSubview(label)
        return label
    }()
    
    lazy var rightImageView1: UIImageView = {
        let view = UIImageView(image: UIImage(named: "Mine_More"))
        addSubview(view)
        return view
    }()
    
    lazy var managerBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(onClickButton(_:)), for: .touchUpInside)
        addSubview(button)
        return button
    }()
    
    private lazy var lineView5: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        addSubview(view)
        return view
    }()
    
    // 职位
    private lazy var positionLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "职位"
        label.backgroundColor = .clear
        label.textAlignment = .left
        addSubview(label)
        return label
    }()
    
    private lazy var positionMessLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = ""
        label.backgroundColor = .clear
        label.textAlignment = .right
        addSubview(label)
        return label
    }()
    
    lazy var rightImageView2: UIImageView = {
        let view = UIImageView(image: UIImage(named: "Mine_More"))
        addSubview(view)
        return view
    }()
    
    lazy var positionBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(onClickButton(_:)), for: .touchUpInside)
        addSubview(button)
        return button
    }()
    
    private lazy var lineView6: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        addSubview(view)
        return view
    }()
    
    lazy var saveBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle(NSLocalizedString("保存", tableName: "GridLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.titleLabel!.font = Theme.Font.size(16, bold: true)
        button.addTarget(self, action: #selector(onClickSaveButton), for: .touchUpInside)
        button.backgroundColor  = UIColor(hex6: 0x1E90FF)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 20
        addSubview(button)
        return button
    }()
    
    private func setupUI() {
        backgroundColor = .clear
        let margTop: Float = 15
        let margRight: Float = -20
        let margLeft: Float = 10
        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.right.equalToSuperview().offset(margRight)
            make.height.equalTo(20)
        }
        
        lineView1.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView1.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        
        nameTextField.snp.makeConstraints { make in
            make.centerY.equalTo(nameLabel.snp.centerY)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        
        lineView2.snp.makeConstraints { make in
            make.top.equalTo(nameLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        phoneLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView2.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        
        phoneTextField.snp.makeConstraints { make in
            make.centerY.equalTo(phoneLabel.snp.centerY)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        
        lineView3.snp.makeConstraints { make in
            make.top.equalTo(phoneLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        senderLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView3.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        
        segmentedSender.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(margRight)
            make.height.equalTo(30)
            make.centerY.equalTo(senderLabel.snp.centerY)
        }
        
        lineView4.snp.makeConstraints { make in
            make.top.equalTo(senderLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        // 所属网格
        managerLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView4.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        
        rightImageView1.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(margRight)
            make.centerY.equalTo(managerLabel.snp.centerY)
            make.width.equalTo(8)
            make.height.equalTo(14)
        }
        
        managerMessLabel.snp.makeConstraints { make in
            make.right.equalTo(rightImageView1.snp.left).offset(-3)
            make.height.equalTo(20)
            make.centerY.equalTo(managerLabel.snp.centerY)
            make.left.equalTo(managerLabel.snp.right)
        }
        
        managerBtn.snp.makeConstraints { make in
            make.top.equalTo(lineView4.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(lineView5.snp.top).offset(-margTop)
        }
        
        lineView5.snp.makeConstraints { make in
            make.top.equalTo(managerLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        // 职位
        positionLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView5.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        
        rightImageView2.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(margRight)
            make.centerY.equalTo(positionLabel.snp.centerY)
            make.width.equalTo(8)
            make.height.equalTo(14)
        }
        
        positionMessLabel.snp.makeConstraints { make in
            make.right.equalTo(rightImageView2.snp.left).offset(-3)
            make.height.equalTo(20)
            make.centerY.equalTo(positionLabel.snp.centerY)
            make.left.equalTo(positionLabel.snp.right)
        }
        
        positionBtn.snp.makeConstraints { make in
            make.top.equalTo(lineView5.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(lineView6.snp.top).offset(-margTop)
        }
        
        lineView6.snp.makeConstraints { make in
            make.top.equalTo(positionLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        // 保存
        saveBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(26)
            make.right.equalToSuperview().offset(-26)
            make.height.equalTo(40)
            make.bottom.equalToSuperview().offset(-80)
        }
        
    }
    
    @objc func segmentedControlChange(_ segmented: UISegmentedControl) {
        delegate?.gridPersonnelAddViewClickGender(tag: segmented.selectedSegmentIndex)
    }
    
    @objc func nameTextFieldDidChange(tf: UITextField) {
        if tf == nameTextField {
            delegate?.gridPersonnelAddViewTextFieldChange(text: tf.text, type: 1)
        }else {
            delegate?.gridPersonnelAddViewTextFieldChange(text: tf.text, type: 2)
        }
    }
    
    @objc func onClickButton(_ sender: UIButton) {
        if sender == managerBtn {
            delegate?.gridPersonnelAddViewSelectedClick(type: 1)
        }else {
            delegate?.gridPersonnelAddViewSelectedClick(type: 2)
        }
    }
    
    // 保存
    @objc func onClickSaveButton() {
        delegate?.gridPersonnelAddViewSaveClick()
    }
}
